<template>
  <div v-drag>
    <div class="mask" @click="closePopup(false)"></div>
    <div class="edit_box" v-loading="dataLoad">
      <div class="box_head">
        <p>{{managerId?'修改':'新增'}}管理员 </p>
        <i @click="closePopup(false)"></i>
      </div>
      <div class="box_body" >
        <div class="line" :class="{'error_border':tipNum===0}">
          <span class="lab"><i class="mark_icon">*</i>名称：</span>
          <div>
            <el-input type="text" placeholder="请输入名称"  v-model.trim="managerName"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <div class="line" :class="{'error_border':tipNum===1}">
          <span class="lab"><i class="mark_icon">*</i>手机号：</span>
          <div>
            <el-input type="text" placeholder="请输入手机号"  v-model.trim="managerPhone"
                      @input="setTip('','')"></el-input>
          </div>
        </div>
        <p class="error_line">{{infoTip}}</p>
      </div>
      <div class="box_foot">
        <span @click="closePopup(false)">取消</span>
        <span @click="confirmEdit">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
import commons from "@/utils/common";
import {addFireBuildingManager,uupdateFireBuildingManager} from "../../../api/manager";

export default {
  props:['manager'],
  data(){
    return{
      managerId:'',
      managerName:'',
      managerPhone:'',
      dataLoad:false,
      infoTip:'',
      tipNum:'',
    }
  },
  mounted() {
    if (this.manager){
      this.managerId = this.manager.id;
      this.managerName = this.manager.name;
      this.managerPhone = this.manager.phone;
    }
  },
  methods:{
    confirmEdit(){
      if (this.managerName === '') {
        this.setTip(0, '请输入管理员名称！')
      } else if (commons.judgePhone(0,this.managerPhone)) {
        this.setTip(1, '请输入确正电话号码！');
      } else {
        this.sendInfo();
      }
    },

    sendInfo() {
      let that = this;
      that.dataLoad = true;
      let data = {
        phone: that.managerPhone,
        name: that.managerName,
      }
      if (that.managerId) {
        data.id = that.managerId;
        uupdateFireBuildingManager(JSON.stringify(data)).then(response => {
          that.setResult(response.success);
        }).catch((e) => {
          that.dataLoad = false;
        });
      } else {
        addFireBuildingManager(JSON.stringify(data)).then(response => {
          that.setResult(response.success);
        }).catch((e) => {
          that.dataLoad = false;
        });
      }
    },

    setResult(finish){
      if (finish){
        this.closePopup(true);
        this.$message.success('操作成功！');
      }
      this.dataLoad = false;
    },


    setTip(num,text){
      this.tipNum = num;
      this.infoTip = text;
    },

    closePopup(refresh){
      this.$emit('listenToChild', {refresh: refresh})
    }
  }
}
</script>

<style scoped>

</style>
